package com.neuedu.funlifebbs.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import com.neuedu.funlifebbs.bean.Categorys;
import com.neuedu.funlifebbs.bean.Labels;
import com.neuedu.funlifebbs.bean.Topics;
import com.neuedu.funlifebbs.bean.Users;
import com.neuedu.funlifebbs.service.CategorysService;
import com.neuedu.funlifebbs.service.LabelsService;
import com.neuedu.funlifebbs.service.TopicsServic;
import com.neuedu.funlifebbs.service.UsersService;
import com.neuedu.funlifebbs.utils.StringUtil;

/**
 * 
 * @author myh
 * @version 2019年8月21日上午10:07:34
 *
 */
@Controller
@RequestMapping("/")
public class IndexController {

	@Autowired
	UsersService usersService;

	@Autowired
	CategorysService categorysService;

	@Autowired
	TopicsServic topicsServic;
	
	@Autowired
	LabelsService labelsService;

	@RequestMapping("")
	public String indexUi(Model model) {
		List<Categorys> list = categorysService.findAll();

		model.addAttribute("list", list);
		return "index";
	}
	
	@RequestMapping(value = "login", method = RequestMethod.GET)
	public String login() {
		return "login";
	}

	@RequestMapping(value = "login", method = RequestMethod.POST)
	public String login(Model model, String name, String password,
			HttpSession session) {
		String errorMsg;
		if (StringUtil.isEmpty(name) || StringUtil.isEmpty(password)) {
			errorMsg = "账号密码不能为空";
		} else {
			Users user = usersService.login(name, password);
			if (user == null) {
				errorMsg = "账号或密码错误";
			} else {
				session.setAttribute("users", user);
				return "redirect:/";
			}
		}
		model.addAttribute("errorMsg", errorMsg);
		return "login";
	}

	@RequestMapping(value = "community/{id}", method = RequestMethod.GET)
	public String community(Model model, @PathVariable int id) {
		// 查标签
		List<Labels> list = labelsService.findAll();
		model.addAttribute("list", list);

		// 查帖子
		List<Topics> tList = topicsServic.findByCategorysId(id);
		model.addAttribute("tList", tList);
		System.out.println(id);
		return "community";
	}

	@RequestMapping(value = "collection", method = RequestMethod.GET)
	public String adlogincollection() {

		return "front/collection";
	}

	@RequestMapping(value = "pim", method = RequestMethod.GET)
	public String pim() {
		return "front/pim";
	}

	@RequestMapping(value = "edituser", method = RequestMethod.POST)
	public String edituser(Users users, HttpServletRequest request,
			MultipartFile pictureFile) {

		// 使用UUID给图片重命名，并去掉四个“-”
		String name = UUID.randomUUID().toString().replaceAll("-", "");
		// 获取文件的扩展名
		String ext = FilenameUtils.getExtension(pictureFile
				.getOriginalFilename());
		// 设置图片上传路径
		String url = request.getSession().getServletContext()
				.getRealPath("/upload");
		System.out.println(url);
		// 以绝对路径保存重名命后的图片
		try {
			pictureFile.transferTo(new File(url + "/" + name + "." + ext));
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 把图片存储路径保存到数据库
		users.setPicture("upload/" + name + "." + ext);
		System.out.println(users.getId());
		if (users.getId() != 0) {
			usersService.update(users);

		} else {
			return "login";
		}
		return "front/collection";
	}
}
